<?php 
if(KT!='KhiThe') exit;
class links_controller extends links_model{
		
	public function __construct(){
		global $lang;
		$this->lang = $lang;
		$this->lang['sms'] ='';
		parent::__construct();
		parent::addObject('khithe_template');
		parent::addObject('khithe_string');
		parent::addObject('khithe_request');
		parent::addObject('khithe_upload');
	}
	
	public function index_action()
	{
		$resultHtml = '';
		$strHtmlContent = $this->khithe_display('index');
		$arrBlockHtml = $this->khithe_getBlockHtml($strHtmlContent);
		
		$resultlink = parent::getlinks();
		while( $row_links = mysql_fetch_assoc($resultlink) )
		{
			if( $row_links['Order']==1 ) $vitri = $this->lang['Left'];
			else if ( $row_links['Order']==2 ) $vitri = $this->lang['Right'];
			else if ( $row_links['Order']==3 ) $vitri = $this->lang['Center'];
				
			$arrlink = array(
				"cell_ID"=>$row_links['ID'],
				"cell_1"=>'<input name="delId[]" type="checkbox" value="'.$row_links['ID'].'" />',
				"cell_2"=>$row_links['Title'],
				"cell_3"=>parent::cutString($row_links['Link'],36),
				"cell_3t"=>$row_links['Link'],
				"cell_4"=>'<img width="98" height="40" border="0" src="'.url_base.'uploads/links/'.$row_links['Picture'].'" />',
				"cell_5"=>$vitri,
				"cell_6"=>$row_links['Status']=='true'?$this->lang['enabled']:$this->lang['disabled'],
				"class_cell"=>$row_links['Status']=='true'?"cell_groupitem1":"cell_groupitem_ex1",
			);
			$resultHtml .= $this->khithe_assign($arrBlockHtml['links'],$arrlink);
			
		} mysql_free_result($resultlink);
		
		$strHtmlContent = $this->khithe_assignBlock($strHtmlContent, array('htmlCat'=>$resultHtml));
		$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
		return $strHtmlContent;
	}
	
	public function add_action()
	{
		if($_GET['iframe']=='true')
		{
			if(isset($_POST['txt_links']))
			{
				$_POST['Picture'] = $this->upload_img($_FILES['txt_PicLink'],path_base.'/uploads/links/',1000000);					
				$linkname = parent::checkStrMySQL($_POST['txt_links']);					
				if($linkname==''){
					$this->language['sms'] = $this->language['Emptylink'];
				
				}else{
					$done = parent::updateInsertlink($_POST);
					if($done) parent::gotowinparent(url_base.'admin/links');
				}
			}
			$this->lang['txt_linksNameValue'] = isset($_POST['txt_linksName'])?$_POST['txt_linksName']:NULL;
			$this->lang['txt_linksValue'] = isset($_POST['txt_links'])?$_POST['txt_links']:NULL;
			$oderlink = isset($_POST['orderLink'])?$_POST['orderLink']:NULL;
			$this->lang['select_Left'] =($oderlink=='1'?' selected="selected" ':'');
			$this->lang['select_Right'] =($oderlink=='2'?' selected="selected" ':'');
			$this->lang['select_Center'] =($oderlink=='3'?' selected="selected" ':'');
			$this->lang['active'] ='add';
			$this->lang['linksID'] ='';
			$this->lang['titlePageEditor'] = $this->lang['addTitle'];
			$strHtmlContent = $this->khithe_display('editor');
			$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
			return $strHtmlContent;
		}
		
	}
	
	public function edit_action()
	{
		if($_GET['iframe']=='true')
		{
			if(isset($_POST['txt_links']))
			{
				$_POST['Picture'] = $this->upload_img($_FILES['txt_PicLink'],path_base.'/uploads/links/',1000000);					
				$linkname = parent::checkStrMySQL($_POST['txt_links']);					
				if($linkname==''){
					$this->language['sms'] = $this->language['Emptylink'];
				
				}else{
					$done = parent::updateInsertlink($_POST);
					if($done) parent::gotowinparent(url_base.'admin/links');
				}
			}
			$resultlink = parent::getlinkID($this->valUrl(1));
			$row_links = mysql_fetch_assoc($resultlink);
			$strHtmlContent = $this->khithe_display('editor');
			$this->lang['titlePageEditor'] = isset($this->lang['editTitle'])?$this->lang['editTitle']:NULL;
			$this->lang['txt_linksNameValue'] = isset($_POST['txt_linksName']) && $_POST['txt_linksName']!=''?$_POST['txt_linksName']:$row_links['Title'];
			$this->lang['txt_linksValue'] = isset($_POST['txt_links']) && $_POST['txt_links']!=''?$_POST['txt_links']:$row_links['Link'];
			$oderlink = isset($_POST['orderLink'])?$_POST['orderLink']:NULL;
			$this->lang['select_Left'] =($row_links['Order']=='1' || $oderlink=='1'?' selected="selected" ':'');
			$this->lang['select_Right'] =($row_links['Order']=='2' || $oderlink=='2'?' selected="selected" ':'');
			$this->lang['select_Center'] =($row_links['Order']=='3' || $oderlink=='2'?' selected="selected" ':'');
			$this->lang['active'] ='edit';
			$this->lang['linksID'] =$row_links['ID'];
			$this->lang['pic'] =$row_links['Picture'];
			$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
			return $strHtmlContent;
		}
		
	}
	
	public function index_ajax_action()
	{
		switch ($this->valUrl(1))
		{
			case 'open':
				$arr = explode(',',$this->valUrl(2));
				foreach ( $arr as $ID ){
					$select = parent::getlinkID($ID);
					while($row = mysql_fetch_assoc($select)){
						if($row['Status']=='false'){
							$done = parent::setCheckedlink($ID,'true');
						}
					}
				}
			break;
			
			case 'close':
				$arr = explode(',',$this->valUrl(2));
				foreach ( $arr as $ID ){
					$select = parent::getlinkID($ID);
					while($row = mysql_fetch_assoc($select)){
						if($row['Status']=='true'){
							$done = parent::setCheckedlink($ID,'false');
						}
					}
				}
			break;
			
			case 'del':
				$arr = explode(',',$this->valUrl(2));
				foreach ( $arr as $ID ){
					$done = parent::delCheckedlink($ID);	
				}
			break;
			
		}
		if($this->valUrl(1)!=''&&isset($done)){
			return $this->index_action();
			
		}else{
			return 'notOK';
		}
	}


}
?>